<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-form :inline="true" :model="queryinfo" class="demo-form-inline" size="mini">
        <el-form-item >
          <el-input v-model="queryinfo.username" placeholder="用户昵称" clearable/>
        </el-form-item>
        <el-form-item >
          <el-input v-model="queryinfo.account" placeholder="用户账号" clearable/>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryinfo.state" clearable filterable>
            <el-option label="正常" value="1"></el-option>
            <el-option label="拉黑" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="注册时间">
          <el-date-picker
              v-model="timer"
              type="datetimerange"
              range-separator="-"
              start-placeholder="Start date"
              end-placeholder="End date"
              :default-time="defaultTime"
              @change="changeData"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="GetData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="DataOpen(1)">创建</el-button>
        </el-form-item>
      </el-form>
      <hr>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="username" label="用户名" width="180" />
        <el-table-column prop="account" label="账号" width="180" />
        <el-table-column prop="state" label="状态" width="180" >
          <template #default="props">
            <el-tag size="mini" v-if="props.row.state === 1" type="success">正常</el-tag>
            <el-tag size="mini" v-else type="warning">拉黑</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="addtime" label="注册时间" >
          <template #default="props">{{ formatDate(props.row.addtime) }}</template>
        </el-table-column>
        <el-table-column prop="uptime" label="更新时间" >
          <template #default="props">{{ formatDate(props.row.uptime) }}</template>
        </el-table-column>

        <el-table-column label="##">
          <template #default="props">
            <el-button type="primary" @click="DataOpen(props.row)" size="mini">Edit</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
          background
          :current-page="queryinfo.page"
          :page-sizes="[3,10, 50, 100, 1000]"
          :page-size="queryinfo.size"
          layout="total, sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="changePage"
      />
    </el-card>

    <el-drawer
        v-model="DataDrawer"
        :title="DataType===1?'添加':'更新'"
        direction="rtl"
        :before-close="DataClose"
    >
      <el-form  :model="DataForm" label-width="80px">
        <el-form-item label="用户账号">
          <el-input v-model="DataForm.account" :disabled="DataType===1?false:true"></el-input>
        </el-form-item>
        <el-form-item label="用户名称">
          <el-input v-model="DataForm.username" :disabled="DataType===1?false:true"></el-input>
        </el-form-item>
        <el-form-item label="用户密码">
          <el-input v-model="DataForm.password"></el-input>
        </el-form-item>
        <el-form-item label="用户状态">
          <el-radio v-model="DataForm.state" :label='1'>正常</el-radio>
          <el-radio v-model="DataForm.state" :label='2'>拉黑</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="DataSubmit">{{ DataType === 1? "Create" : "Updata" }}</el-button>
          <el-button @click="DataClose">Clone</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

  </div>
</template>

<script>
import {onMounted, reactive, toRefs} from "vue";
import {ElMessage} from "element-plus";
import axios from "@/utils/axios";
import {formatDate} from '@/utils/date'

export default {
  name: "user",
  setup() {
    const state = reactive({
      queryinfo: {
        page: 1,
        size: 10
      },
      currentPage: 1, // 当前页
      pageSize: 10, // 分页大小
      total: 0,
      tableData: [],

      timer:[],
      defaultTime: [
        new Date(2000, 1, 1, 0, 0, 0),
        new Date(2000, 2, 1, 23, 59, 59),
      ],

      //更新添加
      DataDrawer: false,
      DataForm:{},
      DataType:1, // 1添加 2 更新

    })
    onMounted(() => {
      GetData()
    })
    //时间操作
    const changeData = async (row) => {
      if (row !== null) {
        state.queryinfo.startTime = ((new Date(row[0])).getTime()) / 1000
        state.queryinfo.endTime = ((new Date(row[1])).getTime()) / 1000
      } else {
        delete state.queryinfo.startTime
        delete state.queryinfo.endTime
      }
    }

    //抽屉操作
    const DataOpen = (data) => {
      state.DataDrawer = true
      if (data === 1){
        state.DataType = 1

        state.DataForm.state=1

      }else{
        state.DataType = 2
        state.DataForm = data
      }
    }
    const DataClose = () => {
      state.DataDrawer = false
      state.DataForm = {}
    }
    const DataSubmit = async () => {
      if (state.DataType === 1){
        const {data: res} = await axios.put('user', state.DataForm)
        if (res.code !== 1000) return ElMessage.error(res.msg)
      }else{
        const {data: res} = await axios.post('user', state.DataForm)
        if (res.code !== 1000) return ElMessage.error(res.msg)
      }
      ElMessage.success("成功")
      GetData()
      DataClose()
    }








    /////////////////////////////////////  基操  /////////////////
    // 获取列表
    const GetData = async () => {
      const {data: res} = await axios.get('user', {params: state.queryinfo})
      if (res.code !== 1000) return ElMessage.error(res.msg)
      state.tableData = res.data.list
      state.total = res.data.total
    }
    const changePage = (val) => {
      state.queryinfo.page = val
      GetData()
    }
    const handleSizeChange = (val) => {
      state.queryinfo.size = val
      GetData()
    }

    return {
      ...toRefs(state),
      GetData,
      formatDate,
      changePage,
      handleSizeChange,
      changeData,
      DataClose,
      DataOpen,
      DataSubmit
    }
  }
}
</script>

<style scoped>

</style>